package com.example.bee_work_3.account;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bee_work_3.net_request.MyJsonObject;
import com.example.bee_work_3.R;
import com.example.bee_work_3.net_request.WS;

public class RegisterActivity extends Activity {

    private LoginOrRegister loginOrRegister;
    private EditText username, password, rePassword;
    private Button register;
    private String user_text = "";

    private WS.CallBack callBack = new WS.CallBack() {
        @Override
        public void onResult(String Json) {
            Log.e("register", Json);
            final MyJsonObject temp = new MyJsonObject(Json);
            if (temp.getInt("errorCode") == 0) {
                Intent data = new Intent(RegisterActivity.this, LoginActivity.class);
                data.putExtra("username", user_text);
                data.putExtra("password", password.getText().toString());
                setResult(RESULT_OK, data);
                finish();
                startActivity(data);
            } else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), temp.getString("errorMsg"), Toast.LENGTH_SHORT).show();
                    }
                });
                return;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }

    public void init() {
        SharedPreferences sharedPreferences = getSharedPreferences("user_data", MODE_PRIVATE);
        loginOrRegister = new LoginOrRegister(sharedPreferences, callBack);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        rePassword = (EditText) findViewById(R.id.re_password);
        register = (Button) findViewById(R.id.login);
        rePassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null) {
                    register.callOnClick();
                    return true;
                }
                return false;
            }
        });
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Loading...", Toast.LENGTH_SHORT).show();
                user_text = username.getText().toString();
                String user_text = username.getText().toString();
                String pwd_text = password.getText().toString();
                String re_pwd_text = rePassword.getText().toString();
                if (user_text.equals("") || pwd_text.equals("")) {
                    Toast.makeText(getApplicationContext(),
                            "账号密码不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!pwd_text.equals(re_pwd_text)) {
                    Toast.makeText(getApplicationContext(), "密码不一致", Toast.LENGTH_SHORT).show();
                    rePassword.setText("");             //清空rePassword
                }
                loginOrRegister.register(user_text, pwd_text);
            }
        });

    }

}
